<template>
	<div>
		<div style="height:10px"></div>

		<van-field v-model="params.code" name="激活码" label="激活码" placeholder="请输入" maxlength="32"
			:rules="[{ required: true, message: '请填写激活码' }]" />

		<van-button class="info-submit" round type="primary" @click="submit">激活</van-button>
	</div>
</template>
<script>
import axios from 'axios'
import { Toast } from 'vant';
export default {
	data() {
		return {
			params: {
				code: ''
			},
		}
	},
	mounted() {

	},
	methods: {
		async submit() {
			const accode = this.params.code
			console.log(this.params)
			if (!accode || accode.length !== 32) {
				Toast.fail('请输入有效的激活码');
				return
			}
			const url = '/activate';
			var tokenInfo = localStorage.getItem('tokenInfo')
			if (tokenInfo) {
				tokenInfo = tokenInfo.substring(0, tokenInfo.length - 1) + `,"code":"` + accode + `"}`
			}
			try {
				const ax = axios.create({
					timeout: 5000 // 指定请求的最大等待时间为 5 秒
				});

				const resp = await ax.post(url, tokenInfo, {
					headers: {
						'Content-Type': 'application/json', // 指定请求头的 Content-Type 为 JSON
					},
				}).then(resp => {
					// console.log('resp', resp)
					if (resp.status === 200 && resp.data.code === 200) {
						var tokenInfo = JSON.parse(localStorage.getItem('tokenInfo'))
						tokenInfo.deadline = resp.data.deadline
						console.log('tokenInfo', tokenInfo)
						localStorage.setItem('tokenInfo', JSON.stringify(tokenInfo))
						console.log('激活成功')
						Toast.success(resp.data.msg)
					} else {
						console.log('激活异常', resp.data.msg)
						Toast.fail('激活失败：' + resp.data.msg)
					}
				})
			} catch (error) {
				// 处理请求错误
				console.log('激活请求错误：', error);
				Toast.fail('激活请求错误：' + error)
			}
		}
	}
}
</script>

<style lang="less" scoped>
.info-submit {
	background: #0074FF;
	width: 80vw;
	margin: 5vw auto !important;
}

.info-img {
	line-height: 50px !important;

	.van-cell__value {
		height: 50px !important;
	}
}
</style>